function reset() {
  var dim = 8;
  
  // Set up the playing field
  var sheet = SpreadsheetApp.getActiveSheet();
  
  var i = 0;
  var columns = ['A', 'B', 'C', 'E', 'F', 'G', 'H', 'I'];
  
  for (i = 0; i < dim; i++) {
      sheet.setColumnWidth(i+1, 20);
      sheet.setRowHeight(i+1, 20);
  }

  sheet.getRange(1, 1, dim, dim).setValue(' ').setBackground('black');

  // Set up player legend
  sheet.setColumnWidth(dim+2, 20);
  sheet.getRange(4, dim+2, 1, 1).clear().setBackground('blue');
  sheet.getRange(4, dim+3, 1, 1).setValue('Player 1');
  sheet.getRange(5, dim+2, 1, 1).clear().setBackground('red');
  sheet.getRange(5, dim+3, 1, 1).setValue('Player 2');
  sheet.getRange(7, dim+2, 1, 1).clear().setBackground('blue');
  sheet.getRange(8, dim+2, 1, 1).clear().setBackground('red');
  sheet.getRange(7, dim+3, 1, 1).setValue('0');
  sheet.getRange(8, dim+3, 1, 1).setValue('0');

  sheet.getRange(2, dim+3, 1, 1).setValue("Player 1's turn").setFontWeight('normal');
}
  
function onOpen() {
  // Set up menu
  var subMenu = [{name:"Take turn", functionName: "takeTurn"},
          {name:"Reset", functionName: "reset"}];

  SpreadsheetApp.getActiveSpreadsheet().addMenu("Paint", subMenu);
}
  
function takeTurn() {
  var dim = 8;

  // Update the board
  var sheet = SpreadsheetApp.getActiveSheet();
  var col = sheet.getActiveRange().getColumnIndex();
  var row = sheet.getActiveRange().getRowIndex();

  // Make sure we're on the board
  if (col > dim || row > dim) {
    return;
  }
        
  // Make sure the square is black
  if(sheet.getActiveRange().getBackgroundColor() != 'black') {
    return;
  }
        
  // Find out who's turn it is
  var turn = sheet.getRange(2, dim+3, 1, 1).getValue();
    
  if (turn == "Player 1's turn") {
    setColor(sheet, row, col, 'blue');
    setColor(sheet, row-1, col, 'blue');
    setColor(sheet, row-2, col, 'blue');
    setColor(sheet, row+1, col, 'blue');
    setColor(sheet, row+2, col, 'blue');
    
    sheet.getRange(2, dim+3, 1, 1).setValue("Player 2's turn");
    
  } else {
    setColor(sheet, row, col, 'red');
    setColor(sheet, row, col-1, 'red');
    setColor(sheet, row, col-2, 'red');
    setColor(sheet, row, col+1, 'red');
    setColor(sheet, row, col+2, 'red');
    
    sheet.getRange(2, dim+3, 1, 1).setValue("Player 1's turn");
  }

  // Update the count for each player
  var colors = sheet.getRange(1, 1, dim, dim).getBackgroundColors();
  var blue_count = 0;
  var red_count = 0;

  for (var i in colors) {
    for (var j in colors[i]) {
      if (colors[i][j] == 'blue') {
        blue_count += 1;
      } else if (colors[i][j] == 'red') {
        red_count += 1;
      }
    }
  }

  sheet.getRange(7, dim+3, 1, 1).setValue(blue_count);
  sheet.getRange(8, dim+3, 1, 1).setValue(red_count);

  // Check if game is over
  if (blue_count + red_count == dim * dim) {
    if (blue_count > red_count) {
      sheet.getRange(2, dim+3, 1, 1).setValue("Player 1 wins!").setFontWeight('bold');
    } else if (red_count > blue_count) {
      sheet.getRange(2, dim+3, 1, 1).setValue("Player 2 wins!").setFontWeight('bold');
    }
    else {
      sheet.getRange(2, dim+3, 1, 1).setValue("It's a tie!");
    }
  }
}
    
function setColor(sheet, row, col, color) {
  var dim = 8;

  if (row < 1 || row > dim) {
    return;
  }
      
  if (col < 1 || col > dim) {
      return;
  }
  
  sheet.getRange(row, col).setBackground(color);
}